/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sevent.service;

import com.sevent.model.dao.T01EventDao;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author enriquer
 */
@Service
public class EventServiceImpl implements EventService {

    private static final Log log = LogFactory.getLog(EventServiceImpl.class);
    @Autowired
    private T01EventDao eventDao;

    public Integer saveEvent(Map<String, Object> params) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("saveNewEvent::start");
                log.debug(params);
            }
            Integer eventId = (Integer) params.get("eventId");

            if (eventId == null) {
                //it's a new event
                eventId = eventDao.getSequence();
                log.info("event id: " + eventId);
                params.put("eventId", eventId);
                eventDao.insert(params);
            } else {
                //update an event
                eventDao.update(params);
            }

            return eventId;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new RuntimeException(ex);
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("saveNewEvent::finish");
            }
        }
    }

    public Map<String, Object> findEventById(Integer eventId) {
        //Map<String, Object> event = null;
        try {
            if (log.isDebugEnabled()) {
                log.debug("findEventById::start");
                log.debug(eventId);
            }


            return eventDao.findById(eventId);

        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new RuntimeException(ex);
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("findEventById::finish");
            }
        }
    }

    public List<Map<String, Object>> findEvents(Map<String, Object> params) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("findEvents::start");
                log.debug("params: "+params);
            }

            List<Map<String, Object>> lsEventsResp=new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> lsEvents = eventDao.find(params);
            
            Map<String, Object> eventResp=null;
            Timestamp date=null;
            SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy HH:mm");
            for(Map<String, Object> event:lsEvents){
                eventResp=new HashMap<String, Object>();
                eventResp.put("eventId", event.get("t01_id"));
                eventResp.put("name", event.get("t01_name"));
                eventResp.put("description", StringUtils.defaultString((String)event.get("t01_description")));
                date=(Timestamp)event.get("t01_startdate");
                eventResp.put("startDate", sdf.format(new Date(date.getTime())));
                date=(Timestamp)event.get("t01_enddate");
                eventResp.put("endDate", sdf.format(new Date(date.getTime())));
                lsEventsResp.add(eventResp);
            }
            
            return lsEventsResp;

        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new RuntimeException(ex);
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("findEvents::finish");
            }
        }
    }
}
